package com.icehand.json.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * jackson
 * @author zhoujunmei
 * @date 2019-08-02 10:23
 * @remark
 */
public class JacksonUtil {
    private static ObjectMapper objectMapper = new ObjectMapper();

    public static String toJSONString(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T> T toObject(String string, Class<T> clazz){
        try {
            return objectMapper.readValue(string,clazz);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
